package com.nanhai.buddhist.college.module.collegebase.controller.admin.accommodationbed;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;

import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.nanhai.buddhist.college.module.collegebase.controller.admin.accommodationbed.vo.*;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.accommodationbed.AccommodationBedDO;
import com.nanhai.buddhist.college.module.collegebase.convert.accommodationbed.AccommodationBedConvert;
import com.nanhai.buddhist.college.module.collegebase.service.accommodationbed.AccommodationBedService;

@Api(tags = "管理后台 - 床铺")
@RestController
@RequestMapping("/collegebase/accommodation-bed")
@Validated
public class AccommodationBedController {

    @Resource
    private AccommodationBedService accommodationBedService;

    @PostMapping("/create")
    @ApiOperation("创建床铺")
    public CommonResult<String> createAccommodationBed(@Valid @RequestBody AccommodationBedCreateReqVO createReqVO) {
        return success(accommodationBedService.createAccommodationBed(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新床铺")
    public CommonResult<Boolean> updateAccommodationBed(@Valid @RequestBody AccommodationBedUpdateReqVO updateReqVO) {
        accommodationBedService.updateAccommodationBed(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除床铺")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    public CommonResult<Boolean> deleteAccommodationBed(@RequestParam("id") String id) {
        accommodationBedService.deleteAccommodationBed(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得床铺")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    public CommonResult<AccommodationBedRespVO> getAccommodationBed(@RequestParam("id") String id) {
        AccommodationBedDO accommodationBed = accommodationBedService.getAccommodationBed(id);
        return success(AccommodationBedConvert.INSTANCE.convert(accommodationBed));
    }

    @GetMapping("/list")
    @ApiOperation("获得床铺列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<AccommodationBedRespVO>> getAccommodationBedList(@RequestParam("ids") Collection<String> ids) {
        List<AccommodationBedDO> list = accommodationBedService.getAccommodationBedList(ids);
        return success(AccommodationBedConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得床铺分页")
    public CommonResult<PageResult<AccommodationBedRespVO>> getAccommodationBedPage(@Valid AccommodationBedPageReqVO pageVO) {
        PageResult<AccommodationBedDO> pageResult = accommodationBedService.getAccommodationBedPage(pageVO);
        return success(AccommodationBedConvert.INSTANCE.convertPage(pageResult));
    }


    @GetMapping("/export-excel")
    @ApiOperation("导出床铺 Excel")
    @OperateLog(type = EXPORT)
    public void exportAccommodationBedExcel(@Valid AccommodationBedExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AccommodationBedDO> list = accommodationBedService.getAccommodationBedList(exportReqVO);
        // 导出 Excel
        List<AccommodationBedExcelVO> datas = AccommodationBedConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "床铺.xls", "数据", AccommodationBedExcelVO.class, datas);
    }

    @GetMapping("/byRoom/list")
    @ApiOperation("根据条件获得床铺列表")
    public CommonResult<List<AccommodationBedRespVO>> getAccommodationBedListByRoomId(@Validated AccommodationBedListReqVO  reqVO) {

        List<AccommodationBedDO> list = accommodationBedService.getAccommodationBedListByRoomId(reqVO);
        return success(AccommodationBedConvert.INSTANCE.convertList(list));
    }
}
